package com.aliyun.messagemgt.application.service.Impl;

import com.aliyun.messagemgt.application.service.ResourceNameService;
import com.aliyun.messagemgt.domain.repository.ResourceNameRepository;
import com.aliyun.messagemgt.common.enums.ResourceTypeEnum;
import com.aliyun.messagemgt.common.exceptions.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.aliyun.messagemgt.common.errorcode.CommonErrorCode.COMMON_PARAM_TYPE_ERROR;

/**
 * Created with IDEA
 *
 * @author:zhangxingxing 0027012120
 * @date:2019/3/20
 * @time:17:23
 */
@Service
public class ResourceNameServiceImpl implements ResourceNameService {

    @Autowired
    private ResourceNameRepository resourceNameRepository;

    @Override
    public String findResourceName(Integer id, ResourceTypeEnum resouceType) {
        return resourceNameRepository.findResourceName(getNameField(resouceType),getTblName(resouceType),getIdField(resouceType),id);
    }

    /**
     * 获取数据库对应的资源名称表字段
     * @param type
     * @return
     */
    private String getNameField(ResourceTypeEnum type){
        switch (type){
            case DATASOURCE:
                return "ds_name";
            case PALN:
                return "plan_name";
            case SCENE:
                return "template_name";
            case MSGSOURCE:
            	return "source_name";
            case PUSHCHANNEL:
                return "description";
            case MONITORINFO:
                return "monitor_name";
            case APPLICATION:
                return "app_name";
            default:
                throw new BaseException(COMMON_PARAM_TYPE_ERROR);
        }
    }

    /**
     * 获取id 列名
     * @param type
     * @return
     */
    private String getIdField(ResourceTypeEnum type){
        switch (type){
            case DATASOURCE:
                return "ds_id";
            case PALN:
                return "plan_id";
            case SCENE:
                return "scene_template_id";
            case MSGSOURCE:
            	return "msg_source_id";
            case PUSHCHANNEL:
                return "push_channel_id";
            case MONITORINFO:
                return "monitor_id";
            case APPLICATION:
                return "app_id";
            default:
                throw new BaseException(COMMON_PARAM_TYPE_ERROR);
        }
    }

    /**
     * 获取表名
     * @param type
     * @returnt
     */
    private String getTblName(ResourceTypeEnum type){
        switch (type){
            case MSGSOURCE:
            	return "msg_source";
            case PUSHCHANNEL:
                return "push_channel";
            default:
                throw new BaseException(COMMON_PARAM_TYPE_ERROR);
        }
    }
}
